#include "Node.h"
#include "NodeContainer.h"


Node::Node(NodeContainer* parent) {
  this->parent=parent;
  unassignedSinks=0;
}

Node::~Node() {
  for (std::vector<Terminal*>::iterator it = terminals.begin(); it!=terminals.end(); ++it) {
    delete *it;
  }
}

bool Node::isIO() {
  return false;
}

void Node::propagateValues() {
  for (std::vector<SourceTerminal*>::iterator it = sources.begin();it!=sources.end();++it) {
    (*it)->propagateValue();
  }
}

void Node::valueAssigned() {
  unassignedSinks--;
  checkForPromotion();
}

void Node::checkForPromotion() {
  if (unassignedSinks==0) {
    parent->promoteChild(this);
  }
}

void Node::addSource() {
  SourceTerminal* term = new SourceTerminal(this);
  sources.push_back(term);
  terminals.push_back(term);
}

void Node::addSink() {
  terminals.push_back(new Terminal(this));
  unassignedSinks++;
}

void Node::connectSourceTo(Node* target, int sourceIndex, int sinkIndex) {
  SourceTerminal* source = (SourceTerminal*)terminals[sourceIndex];
  source->connectSink(target->terminals[sinkIndex]);
}
